package com.flashmoment.video.utils;

import io.jsonwebtoken.*;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Title: JwtUtils
 * @Author xulu
 * @Package com.flashmoment.video.utils
 * @Date 2023/10/25 21:40
 */

public class JwtUtils {

    private static long expireTime = 1000 * 60 * 60 * 24;
    private static String signature = "flashmoment";


    public static String createToken(String email) {
        JwtBuilder jwtBuilder = Jwts.builder();
        String token = jwtBuilder
                .setHeaderParam("type", "jwt")
                .setHeaderParam("alg", "HS256")
                .claim("email", email)
                .setSubject("login")
                .setExpiration(new Date(System.currentTimeMillis() + expireTime))
                .signWith(SignatureAlgorithm.HS256, signature)
                .compact();
        return token;
    }

    public static boolean checkToken(String jwtToken) {
        if (StringUtils.isEmpty(jwtToken)) {
            return false;
        }
        try {
            Jwts.parser().setSigningKey(signature).parseClaimsJws(jwtToken);
        } catch (Exception e) {
            throw e; // 抛出异常交给拦截器处理
        }
        return true;
    }





}
